clear 
use lopnr_peorgnr year avgangsvederlag sp using "$datapath/firsample_aggr"

foreach var of varlist avgangsvederlag sp {
	gen `var'_ihs = log(`var'       + sqrt(`var'^2 +1))
}

winsor2 sp_ihs, cuts(0.1 99.9) by(year) replace
winsor2 avgangsvederlag_ihs, cuts(0.1 99.9) by(year) replace
*/
sum avgangsvederlag_ihs, d
local xm = r(max)
gen x = autocode(avgangsvederlag_ihs,20,0,`xm')

reg sp_ihs avgangsvederlag_ihs 
local b_ihs = round(_b[avgangsvederlag_ihs],.001)
local se_ihs =round( _se[avgangsvederlag_ihs],.001)
local c_ihs = round(_b[_cons],.001)

gcollapse (mean) avgangsvederlag_ihs sp_ihs , by(x)

gen pred = `c_ihs' + `b_ihs'*avgangsvederlag_ihs
sum avgangsvederlag_ihs, d
local rr = r(max)

sort avgangsvederlag_ihs 
twoway /// 
(scatter sp_ihs           avgangsvederlag_ihs, mc(blue) lc(blue) ) ///
(line pred                avgangsvederlag_ihs, lp(-) lc(blue)) ///
(line avgangsvederlag_ihs avgangsvederlag_ihs, lc(black) ) ///
, graphregion(color(white)) legend(off) yt("Imputed Severance Pay (IHS)") xt("Reported Severance Pay (IHS)") ///
 text( 6 3 "{&beta}=0`b_ihs'***" "(0`se_ihs')", place(east)) ///
 text( `rr' `rr' "45{c 176} line", place(east)) ///
 xline(0,lc(gs7)) yline(0,lc(gs7))
graphexportpdf $datapath/sp_ihs, replace

clear 
use lopnr_peorgnr year avgangsvederlag sp using "$datapath/firsample_aggr"

winsor2 sp, cuts(0 99.75) by(year) replace
winsor2 avgangsvederlag, cuts(0 99.75) by(year) replace
sum avgangsvederlag, d
local xm = r(max)
gen x = autocode(avgangsvederlag,20,0,`xm')

reg sp avgangsvederlag 
local b_sek = round(_b[avgangsvederlag],.001)
local se_sek =round( _se[avgangsvederlag],.001)
local c_sek = round(_b[_cons],.001)

gcollapse (mean) avgangsvederlag sp , by(x)

gen pred = `c_sek' + `b_sek'*avgangsvederlag
sum avgangsvederlag, d
local rr = r(max)

sort avgangsvederlag 
twoway /// 
(scatter sp           avgangsvederlag, mc(blue) lc(blue) ) ///
(line pred            avgangsvederlag, lp(-) lc(blue)) ///
(line avgangsvederlag avgangsvederlag, lc(black) ) ///
, graphregion(color(white)) legend(off) yt("Imputed Severance Pay (kSEK)") xt("Reported Severance Pay (kSEK)") ///
 text( 4600 2600 "{&beta}=`b_sek'***" "(0`se_sek')", place(east)) ///
 text( `rr' `rr' "45{c 176} line", place(east)) ///
 xline(0,lc(gs7)) yline(0,lc(gs7))
graphexportpdf $datapath/sp_sek, replace



